package com.example.dicos.controller;


import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.stp.StpUtil;
import com.example.dicos.config.CommonResult;
import com.example.dicos.entity.WxUser;
import com.example.dicos.service.WxUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 后台用户表 前端控制器
 * </p>
 *
 * @author 开发者
 * @since 2022-07-11
 */
@Api(tags = "用户模块")
@RestController
@RequestMapping("/wx")
public class WxUserController extends BaseController {


    @Autowired
    private WxUserService wxUserService;
    @Autowired
    private HttpServletRequest request;

    @ApiOperation("用户登录")
    @GetMapping("/logo/queryName")
    public CommonResult queryName(@RequestParam(value = "userName", defaultValue = "admin", required = true) @ApiParam(required = true) String userName,
                                  @RequestParam(value = "password", defaultValue = "123456", required = true) @ApiParam(required = true) String password) {
        try {
            WxUser wxUser = wxUserService.queryName(userName, password);
            Map<String, Object> map = new HashMap();
            map.put("satoken", wxUser.getSatoken());
            return CommonResult.success(map);
        } catch (Exception e) {
            return CommonResult.failed(e.getMessage());
        }
    }

    @ApiOperation("用户注册")
    @PostMapping("/logo/addUser")
    public CommonResult addUser(@RequestBody WxUser wxUser) {
        try {
            return CommonResult.success(wxUserService.addUser(wxUser));
        } catch (Exception e) {
            return CommonResult.failed(e.getMessage());
        }
    }

    /**
     * 校验权限
     *
     * @param username
     * @return
     */
    public CommonResult list(String username) {

        return wxUserService.list(getUrlStart(request), username);
    }

    /**
     * 根据 请求拦截角色对应的权限
     * 查询信息
     */
//    @SaCheckPermission("/wx/product/index")
//    @GetMapping("/product/index")
    @GetMapping("/selectListWxUser")
    public CommonResult selectListWxUser() {
        return CommonResult.success(wxUserService.selectListWxUser());
    }


    /**
     *
     * 查询全部权限  接口
     * @return
     */
    @GetMapping("/permissionList")
    public CommonResult permissionList() {

        return CommonResult.success(wxUserService.permissionList());
    }

    //获取请求的协议域名，端口号生成连接的前半部分
    public String getUrlStart(HttpServletRequest request) {
        StringBuilder url = new StringBuilder();
        url.append(request.getServletPath());
        return url.toString();
    }


}

